<?php

namespace app\api\controller;

use app\api\model\Goods as GoodsModel;
use app\api\model\Cart as CartModel;
use app\common\service\qrcode\Goods as GoodsPoster;

use app\api\model\People as PeopleModel;
use app\api\model\UploadFile as UploadFileModel;


/**
 * 商品控制器
 * Class Goods
 * @package app\api\controller
 */
class People extends Controller
{
    /**
     * 收货地址列表
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $user = $this->getUser();
        $model = new PeopleModel;
        $list = $model->getList($user['user_id']);
        return $this->renderSuccess([
            'list' => $list,
            'default_id' => $user['address_id'],
        ]);
    }

    /**
     * 信息详情
     * @param $address_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail($people_id)
    {
        $user = $this->getUser();
        $detail = PeopleModel::detail($user['user_id'],$people_id);
        $region = array_values($detail['region']);
        $home = array_values($detail['home']);
        $imageList = [];
        foreach($detail['image'] as $K=>$v){
            $imageList[$K] = $v['file_path'];
        }
        return $this->renderSuccess(compact('detail', 'region','home','imageList'));
    }

    /**
     * 添加
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $model = new PeopleModel;
        $res = $model->add($this->getUser(), $this->request->post());
        if ($res) {
            return $this->renderSuccess([], '添加成功');
        }
        return $this->renderError('添加失败');
    }

    /**
     * 编辑
     * @param $address_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        /*$user = $this->getUser();
        $model = UserAddress::detail($user['user_id'], $address_id);
        if ($model->edit($this->request->post())) {
            return $this->renderSuccess([], '更新成功');
        }
        return $this->renderError('更新失败');*/
        $model = new PeopleModel;
        $res = $model->edit($this->request->post());
        if ($res) {
            return $this->renderSuccess([], '更新成功');
        }
        return $this->renderError('更新失败');

    }

    /**
     * 设为默认地址
     * @param $address_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function setDefault($address_id) {
        $user = $this->getUser();
        $model = UserAddress::detail($user['user_id'], $address_id);
        if ($model->setDefault($user)) {
            return $this->renderSuccess([], '设置成功');
        }
        return $this->renderError('设置失败');
    }

    /**
     * 删除收货地址
     * @param $address_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function delete($address_id)
    {
        $user = $this->getUser();
        $model = UserAddress::detail($user['user_id'], $address_id);
        if ($model->remove($user)) {
            return $this->renderSuccess([], '删除成功');
        }
        return $this->renderError('删除失败');
    }





    public function editImage(){
        $filePath = input('param.filePath');
        $file_id = 0;
        if(strpos($filePath,'uploads') !== false){
            $file_arr = explode('/',$filePath);
            $last_str = $file_arr[count($file_arr) - 1];
            $UploadFileModel = new UploadFileModel();
            $upfile = $UploadFileModel -> where('file_name',$last_str)->find();
            $file_id = $upfile['file_id'];
        }
        return $this->renderSuccess(['file_id'=>$file_id], '添加成功');
    }

}
